package com.dyh.controller;
/*引入Po层*/
import com.dyh.po.role;
/*引入Service层*/
import com.dyh.service.roleService;
import com.dyh.service.menuService;
/*引入spring组件*/
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
/*引入组件*/
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.CharacterCodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * role控制器，用于对角色管理用例进行控制
 */
@Controller
public class roleController {

    /**
     * 使用自动注入标签引用roleService接口及其方法
     */
    @Autowired
    roleService roleservice;
    /**
     * 使用自动注入标签引用menuService接口及其方法
     */
    @Autowired
    menuService menuservice;
    /**
     * @return String
     * @param model
     * 跳转角色列表页
     */
    @RequestMapping(value = "roleList")
    public String list(Model model) {
        List<role> roles = roleservice.findAllRole();
        model.addAttribute("RoleList", roles);
        return "/role/rolelist";
    }
    /**
     * @return String
     * 跳转添加角色页
     */
    @RequestMapping(value = "roleAdd")
    public String toRoleAdd(){
        return "/role/roleAdd";
    }
    /**
     * @return String
     * @param model
     * @param request
     * 添加角色动作
     */
    @RequestMapping(value = "roleAddAction")
    public String addRole(HttpServletRequest request, Model model){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String roleName = request.getParameter("rolename");
        int rows = roleservice.addRole(roleName);
        if (rows>0){
            return "redirect:/roleList";
        }
        return "role/roleAdd";
    }
    /**
     * @return ModelAndView
     * @param request
     * 跳转角色修改页
     */
    @RequestMapping(value = "roleEdit")
    public ModelAndView editRole(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String rid = request.getParameter("r_id");
        Long temp = Long.valueOf(rid);
        role role = roleservice.findRoleById(temp);
        mv.addObject("Role",role);
        mv.setViewName("role/roleEdit");
        return mv;
    }
    /**修改角色动作*/
    @RequestMapping(value = "editRole")
    public void editRole(HttpServletRequest request,Model model){
        String rolename = request.getParameter("rolename");
        String ids = request.getParameter("test");
        System.out.println("rolename="+rolename);
        System.out.println("ids="+ids);
    }

    /**
     * @return Map
     * 查找所有功能
     */
    @RequestMapping(value = "findAllMenu")
    @ResponseBody
    public Object findAllMenu(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("menulists",menuservice.findAllMenu());
        return map;
    }

    /**
     * @return String
     * @param request
     * @param model
     * 删除角色动作
     */
    @RequestMapping(value = "roleDeleteAction")
    public String deleteRole(HttpServletRequest request, Model model){
        String temp = request.getParameter("r_id");
        Long id = Long.valueOf(temp);
        int rows = roleservice.deleteRole(id);
        return "redirect:/roleList";
    }
}
